# coding: utf-8
import sys, os
# 脚本路径
SCRIPT_ROOT_PATH = './'


def patch_sys_path():
    sys_paths = []

    def add_path(path):
        if os.path.isdir(path) and path.find(".svn") == -1:
            sys_paths.append(path)
            sub_paths = os.listdir(path)
            for p in sub_paths:
                abs_path = path + p
                if abs_path[-1] != "/": abs_path += "/"
                add_path(abs_path)

    add_path(SCRIPT_ROOT_PATH)

    for p in sys_paths:
        sys.path.insert(0, p)
    print "patch_sys_path() ok"
patch_sys_path()


# --------------------
import glog, gdb, gtimer
from flask import Flask
from glb import app

# call tabels:
import pages, test_ct
# managers:
import mission_mgr, role_mgr, login, account_mgr

'''
import signal
def sigint_handler(signum, frame): 
    print "sigint_handler"
    sys.exit(0)

signal.signal(signal.SIGINT, sigint_handler)
'''


def main():
    # 日志
    glog.init()
    
    # 数据库
    gdb.init()

    # timer
    gtimer.init()

    # game
    #event_mgr.init()
    mission_mgr.init()

    # test
    test()

    # 开始服务
    print "start server 0.0.0.0:8111"
    app.run(host='0.0.0.0', port=8111, debug=True, use_reloader=False)



def test():
    try:
        account_mgr.create_account('test','test')
        login.on_login(['test', 'test'])

        import item_mgr; item_mgr.test()
        

    except Exception, e:
        raise e

if __name__ == '__main__':
    main()
